<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="商品系列管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="商品分类" prop="goodsClassId" size="mini">
                <el-cascader
                    v-model="formData.cascaderPath"
                    :options="goodsClassList"
                    :props="{ value: 'id',label:'goodsClassName' }"
                    :show-all-levels="false"
                    @change="changeGoodsClass" prop="cascaderPath"></el-cascader>
            </el-form-item>
            <el-form-item label="品牌" prop="brandId" size="mini">
                <el-select v-model="formData.brandId" placeholder="请选择" clearable>
                    <el-option
                        v-for="item in brandList"
                        :key="item.id"
                        :label="item.brandName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="系列名称" prop="designName" size="mini">
                <el-input v-model="formData.designName"/>
            </el-form-item>
            <el-form-item label="图片" prop="image">
                <el-upload
                    class="avatar-uploader"
                    :action="apiHost + '/file/upload'"
                    :show-file-list="false"
                    :on-success="handleSuccess"
                    :headers="headers"
                >
                    <div v-if="formData.image">
                        <img :src="formData.image" class="avatar">
                    </div>
                    <i v-else class="el-icon-plus avatar-uploader-icon"/>
                </el-upload>
                <div class="form-tips">尺寸：100*100</div>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {BrandApi} from '@/api/mall/brand'
import {GoodsClassApi} from '@/api/mall/goodsClass'
import {DesignApi} from '@/api/mall/design'
import {Common} from '@/utils/common'
import { getToken } from '@/utils/auth'

export default {
    name: 'MallBrandUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                goodsClassId: '',
                brandId: '',
                designName: '',
                image: '',
                cascaderPath: []
            },
            rules: {
                cascaderPath: [
                    {required: true, message: '请选择商品分类', trigger: 'change', type: 'array'}
                ],
                designName: [
                    {required: true, message: '请输入品牌名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            goodsClassList: [], // 商品分类
            brandList: [], // 品牌
            apiHost: process.env.VUE_APP_BASE_API,
            headers: { 'X-Token': getToken() }
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true

            BrandApi.getAllList().then((response) => {
                this.brandList = response
            })

            GoodsClassApi.getAllList().then((response) => {
                this.goodsClassList = response

                this.$nextTick(() => {
                    if (this.$refs['formRef'] !== undefined) {
                        this.$refs['formRef'].resetFields()
                    }

                    if (data.id > 0) {
                        data.brandId = data.brandId === 0 ? null : data.brandId
                        this.formData = Common.assign(this.formData, data)
                        this.formData.cascaderPath = Common.getParentNode(this.goodsClassList, this.goodsClassList, this.formData.goodsClassId, []);
                    } else {
                        this.formData.id = 0
                        this.formData.cascaderPath = []
                    }
                })
            })
        },
        handleSuccess(e) {
            this.formData.image = e.data
        },
        changeGoodsClass(e) {
            this.formData.goodsClassId = e[e.length - 1]
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        DesignApi.updateDesign(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        DesignApi.addDesign(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 80px;
    height: 80px;
    line-height: 80px;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
}

.avatar {
    width: 80px;
    height: 80px;
    display: block;
}
</style>
